import {
	baseURL
} from '@/config/index.js'
// 封装请求
function request(url, method = "get", data = {}, header) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseURL + url,
			data,
			method,
			header: {
				'content-type': "application/x-www-form-urlencoded",
				// 'Accept': '*/*',
				// 'Content-Type': 'application/json',//类型
				"Authorization": 'Basic YXBwOmFwcF9zZWNyZXQ=', //请求头
				"Blade-Auth": `bearer ${uni.getStorageSync('token')}`
			},
			success: (res) => {

				if (res.statusCode === 200) {
					switch (res.data.code) {
						case 200:
							resolve(res.data.data);
							break
						case 401:
							uni.showToast({
								icon: 'none',
								title: '登录失效，请重新登录',
								duration: 2000
							});
							uni.redirectTo({
								url: '/pages/login/index'
							})
							break;
						default:
							reject(res.data.msg)
					}
				} else if (res.statusCode === 400) {
					reject(res.data.msg)
				} else if (res.statusCode === 401) {
					uni.showToast({
						icon: 'none',
						title: '登录失效，请重新登录',
						duration: 2000
					});
					uni.redirectTo({
						url: '/pages/login/index'
					})

				} else {
					reject('服务器错误')
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

export {
	request
}